package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.table.*;
import com.xmall.mapper.GoodsMapper;
import com.xmall.mapper.GrouponMapper;
import com.xmall.mapper.GrouponRulesMapper;
import com.xmall.service.GrouponService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GrouponServiceImpl implements GrouponService {

    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public Map<String, Object> queryList(Map<String, String> params) {
        PageHelper.startPage(Integer.parseInt(params.get("page")), Integer.parseInt(params.get("limit")));

        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(params.get("sort") + " " + params.get("order"));
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();

        if (params.get("goodsId") != null && params.get("goodsId").length() != 0 ){
            criteria.andGoodsIdEqualTo(Integer.parseInt(params.get("goodsId")));
        }

        criteria.andDeletedEqualTo(false);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();

        HashMap<String, Object> result = new HashMap<>();
        result.put("total",total);
        result.put("items",grouponRules);
        return result;
    }

    @SneakyThrows
    @Override
    public GrouponRules createGrouponRule(GrouponRules params) {

        Goods goods = goodsMapper.selectByPrimaryKey(params.getGoodsId());

        if (goods == null){
            return null;
        }

        params.setAddTime(new Date());
        params.setUpdateTime(new Date());
        params.setDeleted(false);
        params.setPicUrl(goods.getPicUrl());
        params.setGoodsName(goods.getName());

        grouponRulesMapper.insert(params);

        return params;
    }

    @Override
    public void delete(GrouponRules grouponRules) {
        grouponRules.setDeleted(true);
        update(grouponRules);
    }

    @Override
    public boolean update(GrouponRules grouponRules) {
        Goods goods = goodsMapper.selectByPrimaryKey(grouponRules.getGoodsId());

        if (goods == null){
            return false;
        }
        grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
        return true;
    }

    @Override
    public Map<String, Object> queryListRecord(Map<String, String> params) {
        //todo  瞎写的
        //page=1&limit=20&goodsId=1&sort=add_time&order=desc
        PageHelper.startPage(Integer.parseInt(params.get("page")),Integer.parseInt(params.get("limit")));
        GrouponExample grouponExample = new GrouponExample();
        grouponExample.setOrderByClause(params.get("sort") + " " + params.get("order"));
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        if (params.get("goodsId") != null){
            criteria.andOrderIdEqualTo(Integer.parseInt(params.get("goodsId")));
        }
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        PageInfo<Groupon> grouponPageInfo = new PageInfo<>(groupons);
        long total = grouponPageInfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put("total",total);
        result.put("items",groupons);
        return result;
    }
}
